<?php


namespace App\backend\services;


use App\backend\models\AdminLoginLogModel;
use App\Exceptions\InvalidRequestException;

class AdminLoginLogServices
{

    /**
     * 获取 - 登录日志列表
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function getList($data)
    {
        $page = !isset($data['page']) ? 1 : $data['page'];unset($data['page']);
        $limit = !isset($data['limit']) ? 10 : $data['limit'];unset($data['limit']);

        $where_data = [];
        // 账号类型
        if (isset($data['role_id']) && !empty($data['role_id'])) {
            $where_data[] = ['role_id', '=', $data['role_id']];
        }
        // 登录账号
        if (isset($data['account_name']) && !empty($data['account_name'])) {
            $where_data[] = ['admin_login_log.account_name', 'like', "%{$data['account_name']}%"];
        }
        if (isset($data['member']) && !empty($data['member'])) {
            if (is_numeric($data['member'])) {
                $where_data[] = ['admin_users.id', '=', $data['member']];
                $where_data[] = ['admin_users.phone', '=', $data['member']];
            } else {
                $where_data[] = ['admin_users.username', 'like', "%{$data['member']}%"];
                $where_data[] = ['admin_users.name', 'like', "%{$data['member']}%"];
            }
        }
        $where_between = [];
        if (isset($data['start_time']) && !empty($data['start_time']) && isset($data['end_time']) && !empty($data['end_time']) ) {
            $start_time = strtotime($data['start_time']);
            $end_time = strtotime($data['end_time']);
            $where_between = [$start_time, $end_time];
        }

        $select_data = [
            'admin_login_log.admin_id', 'admin_login_log.ip', 'admin_login_log.device_type', 'admin_login_log.login_at',
            'admin_login_log.account_name', 'admin_login_log.role_name', 'admin_users.name', 'admin_users.avatar'
        ];
        $list = AdminLoginLogModel::listed($page, $limit, $where_data, $where_between, $select_data);
        if (empty($list)) {
            throw new InvalidRequestException(10009);
        }

        foreach ($list as $value) {
            if (is_array($value) && !empty($value)) {
                foreach ($value as $v) {
                    $v->login_at = date('Y-m-d H:i:s', $v->login_at);
                }
            }
        }

        return $list;
    }

    /**
     * 插入 - 登录日志数据
     *
     * @param $login_ip //登录IP
     * @param $login_time //登录时间
     * @return array
     */
    public static function insertLoginData($login_ip, $login_time)
    {
        $user = auth('backend')->user();
        $role = RoleServices::getRoleUser($user['id']);
        $data = [
            'admin_id' => $user['id'],
            'ip' => $login_ip,
            'login_at' => $login_time,
            'device_type' => get_client_os_info(),
            'account_name' => $user['username'],
            'role_name' => $role->name,
            'role_id' => $role->id
        ];
        $list = AdminLoginLogModel::insertGetIdData($data);
        return $list;
    }

}
